package com.jt.tenant.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.bigkoo.pickerview.lib.ArrayWheelAdapter;
import com.bigkoo.pickerview.lib.OnWheelChangedListener;
import com.bigkoo.pickerview.lib.ScreenInfo;
import com.bigkoo.pickerview.lib.WheelView;
import com.jt.tenant.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/28.
 */
public class SelectPopup extends PopupWindow implements View.OnClickListener {
    private View rootView; // 总的布局
    private View btnSubmit, btnCancel;
    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";
    private OnTimeSelectListener timeSelectListener;
    private String temp;
    private List<String> list = new ArrayList<>();
    public SelectPopup(Context context, List<String> mlist) {
        super(context);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new BitmapDrawable());// 这样设置才能点击屏幕外dismiss窗口
        this.setOutsideTouchable(true);
        this.setAnimationStyle(com.bigkoo.pickerview.R.style.timepopwindow_anim_style);
        this.list = mlist;
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        rootView = mLayoutInflater.inflate(R.layout.popup_oil_type, null);

        WheelView wheelView = (WheelView) rootView.findViewById(R.id.year);

        temp = list.get(0);
//        list.add(temp = "93#汽油");
//        list.add("97#汽油");
//        list.add("柴油");
        ScreenInfo screenInfo = new ScreenInfo((Activity) context);
        wheelView.setAdapter(new ArrayWheelAdapter<String>((ArrayList<String>)list));
        wheelView.setCurrentItem(0);
        wheelView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                temp = list.get(newValue);
            }
        });
        wheelView.TEXT_SIZE = (screenInfo.getHeight()/100) * 3;
        // -----确定和取消按钮
        btnSubmit = rootView.findViewById(com.bigkoo.pickerview.R.id.btnSubmit);
        btnSubmit.setTag(TAG_SUBMIT);
        btnCancel = rootView.findViewById(com.bigkoo.pickerview.R.id.btnCancel);
        btnCancel.setTag(TAG_CANCEL);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        setContentView(rootView);
    }

    @Override
    public void onClick(View v) {

        String tag = (String) v.getTag();

        if (tag.equals(TAG_CANCEL)) {

            dismiss();

            return;

        } else {
            if (timeSelectListener != null) {

                timeSelectListener.onTimeSelect(temp);

            }
            dismiss();
            return;
        }
    }
    public void show(View view){

        showAtLocation(view, Gravity.BOTTOM, 0, 0);

    }

    public interface OnTimeSelectListener {
        public void onTimeSelect(String value);
    }

    public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener) {
        this.timeSelectListener = timeSelectListener;
    }

}